package org.easy.aliintersms;

import com.alibaba.fastjson.JSON;
import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;

@Slf4j
public class AlibabaInterSmsTemplate {

    private String appId;
    private String appKey;

    public AlibabaInterSmsTemplate(String appId, String appKey) {
        this.appId = appId;
        this.appKey = appKey;
    }

    public Boolean sendSmsCodeInternational(String phone, Map params, String areaCode, Integer templateId, String smsSign) {
        try {
            //初始化
            Client client = AliyunSmsUtil.createClient(appId, appKey);

            SendSmsRequest sendSmsRequest = new SendSmsRequest()
                    .setSignName(smsSign)
                    .setTemplateCode("SMS_" + templateId)
                    .setPhoneNumbers(areaCode + phone)
                    .setTemplateParam(JSON.toJSONString(params));

            SendSmsResponse response = client.sendSms(sendSmsRequest);

            String codeStr = response.body.code;
            if (!com.aliyun.teautil.Common.equalString(codeStr, "OK")) {
                log.info("错误信息: " + response.body.message + "");
                return false;
            }
            log.info("send success");
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }


}
